package com.zh.web.controller.web.taxi;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.AopLog;
import com.zh.zhbootcommon.config.annotation.VerifyToken;
import com.zh.zhbootcommon.domain.DPage;
import com.zh.zhbootcommon.domain.DResult;
import com.zh.zhbootcommon.entity.taxi.DTaxiOrderInfo;
import com.zh.business.service.taxi.IDTaxiOrderInfoService;
import com.zh.zhbootcommon.utils.code.CreateOrderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 打车订单表 前端控制器
 * </p>
 *
 * @author JueDream
 * @since 2021-08-04
 */
@Slf4j
@VerifyToken
@RestController
@RequestMapping("/to/v1")
@Api(value = "用户打车订单")
@AopLog(type = "用户打车订单", stackTraceOnErr = true)
public class DTaxiOrderInfoController {

    @Autowired
    IDTaxiOrderInfoService orderInfoService;

    @ApiOperation(value = "打车订单查询接口")
    @PostMapping("/queryTaxiOrderPage")
    public DResult queryTaxiOrderPage(@RequestParam int page, @RequestParam int limit, @RequestParam String orderCodeText){
        Page<DTaxiOrderInfo> taxiOrderInfoPage = orderInfoService.queryTaxiOrderPage(page, limit, orderCodeText);
        DPage<DTaxiOrderInfo> taxiOrderInfoDPage = DPage.to(taxiOrderInfoPage);
        return DResult.of(taxiOrderInfoDPage);
    }

    @ApiOperation(value = "打车订单查询单个接口")
    @PostMapping("/queryTaxiOrderOne")
    public DResult queryTaxiOrderOne(@RequestParam Integer orderId){
        return DResult.of(orderInfoService.queryTaxiOrderOne(orderId));
    }

    @Transactional
    @ApiOperation(value = "打车订单编辑接口")
    @PostMapping("/saveOrUpdate")
    public DResult saveOrUpdate(@RequestBody DTaxiOrderInfo taxiOrderInfo) {
        boolean flag = false;
        // 判断订单号
        if (StrUtil.isBlank(taxiOrderInfo.getOrderCode())) {
            taxiOrderInfo.setOrderCode(CreateOrderUtil.generateOrderNo("M-"));
        }
        if (taxiOrderInfo.getOrderId() < 0) {
            // 新增
            flag = orderInfoService.save(taxiOrderInfo);
            // 新增用户订单信息表
        } else if (taxiOrderInfo.getOrderId()  > 0) {
            // 更新
            flag = orderInfoService.updateById(taxiOrderInfo);
            // 更新用户订单信息表
        }
        return DResult.of(flag);
    }

    @ApiOperation(value = "打车订单删除接口")
    @DeleteMapping("/delete")
    public DResult delete(@RequestParam Integer orderId){
        boolean flag = false;
        if (orderId > 0) {
            flag = orderInfoService.removeTaxiOrder(orderId);
        }
        return DResult.of(flag);
    }
}
